package com.elmcode.study.handle;

import com.elmcode.study.exception.ParamsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ParamsException.class)
    public Map<String,String> handleParamException(ParamsException paramsException){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",paramsException.getMessage());
        return info;
    }

    @ExceptionHandler(Exception.class)
    public Map<String,String> handleParamException(Exception e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        return info;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Map<String,String> handlerNotFoundException(Exception e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg","请求路径错误");
        return info;
    }

}
